<?php
function EditLotto ($cmd)
{
    global $conf;
    global $curruser;
    $id = intval(getvar("id"));
    $open = getvar("open");
    $oldopen = getvar("oldopen");
    $stopbet = $conf->get("stopbet");
    $waitbet = $conf->get("waitbet");
    if ($cmd == "edit")
    {
        if ($oldopen == "")
        {
            $oldopen = $open;
        }
        $rs = getdb("!!select * from lotto where id={$id}");
        if ($rs->eof)
        {
            $ui->showErrorPage("<p>找不到该资料!</p>");
            exit();
        }
    }
    if (ispost())
    {
        $round = intval(getvar("round"));
        $spec = intval(getvar("spec"));
        $n1 = intval(getvar("n1"));
        $n2 = intval(getvar("n2"));
        $n3 = intval(getvar("n3"));
        $n4 = intval(getvar("n4"));
        $n5 = intval(getvar("n5"));
        $n6 = intval(getvar("n6"));
        if ($cmd == "edit")
        {
            if ($open == "" || getvar("round") == "")
            {
                diemsg("错误的参数!");
            }
            if (!chknum($spec))
            {
                diemsg("错误的参数!");
            }
            if (!chknum($n1))
            {
                diemsg("错误的参数!");
            }
            if (!chknum($n2))
            {
                diemsg("错误的参数!");
            }
            if (!chknum($n3))
            {
                diemsg("错误的参数!");
            }
            if (!chknum($n4))
            {
                diemsg("错误的参数!");
            }
            if (!chknum($n5))
            {
                diemsg("错误的参数!");
            }
            if (!chknum($n6))
            {
                diemsg("错误的参数!");
            }
            /*if (0 < $n1 && 0 < $n2 && 0 < $n3 && 0 < $n4 && date("Hi", systime() - 60) < $stopbet)
            {
                $conf->set("othebet", date("Hi", systime() - 60));
                $conf->set("stopbet", date("Hi", systime() - 60));
            }
            if (0 < $n1 && date("Hi", systime() - 60) < $waitbet)
            {
                $conf->set("linebet", date("Hi", systime() - 60));
                $conf->set("waitbet", date("Hi", systime() - 60));
            }*/
            if ($n1 + $n2 + $n3 + $n4 + $n5 + $n6 + $spec == 0)
            {
                $sql = "update lotto set open='{$open}',round='{$round}',n1='{$n1}',n2='{$n2}',n3='{$n3}',n4='{$n4}',n5='{$n5}',n6='{$n6}',spec='{$spec}',p1=0,p2=0,p3=0,p4=0,p5=0,p6=0,p7=0,total=0,amount=0,mb=0,ad=0,ua=0,ur=0,rsum=0 where id={$id}";
                getdb($sql);
                getdb("update bet set a1=0,b1=0,a2=0,b2=0,c2=0,d2=0,e2=0,a3=0,b3=0,c3=0,d3=0,e3=0,a4=0,b4=0,c4=0,d4=0,e4=0,a5=0,b5=0,c5=0,d5=0,e5=0,r5=0,g5=0,r6=0,rame=0 where lotto_id=" . $id . " and  rame!=2");
                getdb("update bet2 set a2=0,b2=0,a3=0,b3=0,c3=0,d3=0,e3=0,a4=0,b4=0,c4=0,d4=0,e4=0,a5=0,b5=0,c5=0,d5=0,e5=0,r5=0,g5=0,rame=0 where lotto_id=" . $id . " and  rame!=2");
                getdb("update bet8 set r5=0,rame=0 where lotto_id=" . $id . " and  rame!=2");
            }
            else
            {
                $err = 0;
                if (0 < $n2 && $n2 == $n1)
                {
                    ++$err;
                }
                if (0 < $n3 && ( $n3 == $n1 || $n3 == $n2 ))
                {
                    ++$err;
                }
                if (0 < $n4 && ( $n4 == $n1 || $n4 == $n2 || $n4 == $n3 ))
                {
                    ++$err;
                }
                if (0 < $n5 && ( $n5 == $n1 || $n5 == $n2 || $n5 == $n3 || $n5 == $n4 ))
                {
                    ++$err;
                }
                if (0 < $n6 && ( $n6 == $n1 || $n6 == $n2 || $n6 == $n3 || $n6 == $n4 || $n6 == $n5 ))
                {
                    ++$err;
                }
                if (0 < $spec && ( $spec == $n1 || $spec == $n2 || $spec == $n3 || $spec == $n4 || $spec == $n5 || $spec == $n6 ))
                {
                    ++$err;
                }
                if (0 < $err)
                {
                    echo "<script>alert('号码重复！');</script>";
                }
                else
                {
                    $sql = "update lotto set open='{$open}',round='{$round}',n1='{$n1}',n2='{$n2}',n3='{$n3}',n4='{$n4}',n5='{$n5}',n6='{$n6}',spec='{$spec}' where id={$id}";
                    getdb($sql);
                }
            }
            $rs = getdb("!!select * from lotto where id='{$id}'");
        }
        else
        {
            $rs = getdb("!!select * from lotto where open='{$open}'");
            if (!$rs->eof)
            {
                echo "<p class=error>该开奖日期资料已经存在!</p>";
                echo "<p><input type=button value='回上一页' onclick='history.back()'> ";
                echo "<input type=button value='列表' onclick='location.replace(\"?op=admin&fp=history\")'>";
                echo "</p>";
                exit();
            }
            $cyear = $conf->get("chinayear");
            $sql = "insert into lotto (open,round,cyear) values ('{$open}','{$round}',{$cyear})";
            getdb($sql);
            CreateRate($open, $round);
            $conf->set("protect_start", 1);
            $fields = array();
            $i = 1;
            while ($i < 50)
            {
                $ii = d2($i);
                $temp = 0;
                $fields["r" . $ii] = $temp;
                ++$i;
            }
            $sql = "update maxbet0 set ";
            foreach ($fields as $key => $temp)
            {
                $sql .= " {$key}={$temp}, ";
            }
            if (substr($sql, 0 - 2) == ", ")
            {
                $sql = substr($sql, 0, strlen($sql) - 2);
            }
            $sql .= " WHERE user_id=0";
            getdb($sql);
            redirect("op.php?op=admin&fp=history");
        }
    }
    $tpl = new template2();
    $tpl->load("main", "lotto_edit.htm");
    $tpl->set("id", $id);
    if ($cmd == "edit")
    {
        $tpl->set("cmd", "edit");
        $tpl->set("cmdname", "编辑");
        $tpl->set("open", $rs->f['open']);
        $tpl->set("round", $rs->f['round']);
        $op_mode = "";
        $i = 1;
        while ($i <= 6)
        {
            $tpl->set("n{$i}", $rs->f["n{$i}"]);
            ++$i;
        }
        $tpl->set("spec", $rs->f['spec']);
        if (0 < $rs->f['spec'])
        {
            $tpl->set("calc", "");
        }
        else
        {
            $tpl->set("calc", "disabled");
        }
        if ($curruser['account'] == G_SUPERACCOUNT && $curruser['acl'] == 255)
        {
            $op_mode = "";
        }
        else if (0 < $rs->f['total'])
        {
            $op_mode = "disabled";
        }
        $tpl->set("calc_ok", $op_mode);
        if ($conf->get("waitbet") <= date("Hi"))
        {
            $tpl->set("stopnowdisable1", "disabled");
        }
        if ($conf->get("stopbet") <= date("Hi"))
        {
            $tpl->set("stopnowdisable0", "disabled");
        }
        $rs = getdb("!!select count(amount) from bet where lotto_id=" . $id);
        $rs1 = getdb("!!select count(amount) from bet2 where lotto_id=" . $id);
        $tpl->set("bet_count1", sprintf("%.0f", ( intval($rs->f[0]) + intval($rs1->f[0]) + 1 ) / 360));
        $tpl->set("bet_count2", $rs->f[0] + $rs1->f[0]);
    }
    else
    {
        $tpl->set("round", getmaxround());
        $tpl->set("open", date("Y-m-d"));
        $tpl->set("cmd", "add");
        $tpl->set("cmdname", "新增");
        $tpl->set("bet_count1", 0);
        $tpl->set("bet_count2", 0);
        $tpl->set("calc", "disabled");
        $tpl->set("stopnowdisable1", "disabled");
        $tpl->set("stopnowdisable0", "disabled");
    }
    $tpl->show("main");
}

function chknum ($num)
{
    if ($num < 0)
    {
        return false;
    }
    if (49 < $num)
    {
        return false;
    }
    return true;
}

function GetMaxRound ()
{
    $rs = getdb("select max(round) from lotto where open >= '" . date("Y") . "-01-01'");
    return $rs->f[0] + 1;
}

function CreateRate ($open, $round)
{
    $rs = getdb("!!select id from lotto where open='{$open}' and round='{$round}'");
    $id = $rs->f['id'];
    $prev_id = 0;
    copy_rate("rate0a", $prev_id, $id);
    copy_rate("rate1a", $prev_id, $id);
    copy_rate("rate4a", $prev_id, $id);
    copy_rate("rate5a", $prev_id, $id);
    copy_rate("rate15a", $prev_id, $id);
    copy_rate("rate16a", $prev_id, $id);
    //copy_rate("rate24a", $prev_id, $id);
    //copy_rate("rate31a", $prev_id, $id);
    copy_rate("rate41a", $prev_id, $id);
    copy_rate("rate42a", $prev_id, $id);
    copy_rate("rate43a", $prev_id, $id);
    copy_rate("rate44a", $prev_id, $id);
    copy_rate("rate45a", $prev_id, $id);
    copy_rate("rate46a", $prev_id, $id);
    copy_rate("rate0b", $prev_id, $id);
    copy_rate("rate1b", $prev_id, $id);
    copy_rate("rate4b", $prev_id, $id);
    copy_rate("rate5b", $prev_id, $id);
    copy_rate("rate15b", $prev_id, $id);
    copy_rate("rate16b", $prev_id, $id);
    //copy_rate("rate24b", $prev_id, $id);
    //copy_rate("rate31b", $prev_id, $id);
    copy_rate("rate41b", $prev_id, $id);
    copy_rate("rate42b", $prev_id, $id);
    copy_rate("rate43b", $prev_id, $id);
    copy_rate("rate44b", $prev_id, $id);
    copy_rate("rate45b", $prev_id, $id);
    copy_rate("rate46b", $prev_id, $id);
    copy_rate("rate0c", $prev_id, $id);
    copy_rate("rate1c", $prev_id, $id);
    copy_rate("rate4c", $prev_id, $id);
    copy_rate("rate5c", $prev_id, $id);
    copy_rate("rate15c", $prev_id, $id);
    copy_rate("rate16c", $prev_id, $id);
    //copy_rate("rate24c", $prev_id, $id);
    //copy_rate("rate31c", $prev_id, $id);
    copy_rate("rate41c", $prev_id, $id);
    copy_rate("rate42c", $prev_id, $id);
    copy_rate("rate43c", $prev_id, $id);
    copy_rate("rate44c", $prev_id, $id);
    copy_rate("rate45c", $prev_id, $id);
    copy_rate("rate46c", $prev_id, $id);
    copy_rate("rate0d", $prev_id, $id);
    copy_rate("rate1d", $prev_id, $id);
    copy_rate("rate4d", $prev_id, $id);
    copy_rate("rate5d", $prev_id, $id);
    copy_rate("rate15d", $prev_id, $id);
    copy_rate("rate16d", $prev_id, $id);
    //copy_rate("rate24d", $prev_id, $id);
    //copy_rate("rate31d", $prev_id, $id);
    copy_rate("rate41d", $prev_id, $id);
    copy_rate("rate42d", $prev_id, $id);
    copy_rate("rate43d", $prev_id, $id);
    copy_rate("rate44d", $prev_id, $id);
    copy_rate("rate45d", $prev_id, $id);
    copy_rate("rate46d", $prev_id, $id);
    copy_rate("rate27", $prev_id, $id);
    copy_rate("rate36", $prev_id, $id);
    copy_rate("rate29", $prev_id, $id);
    copy_rate("rate8", $prev_id, $id);
	copy_rate("rate9", $prev_id, $id);
	copy_rate("rate37", $prev_id, $id);
    $table_name = "bettotal_6h_" . $id;
    $sql = "\r\n\tCREATE TABLE IF NOT EXISTS {$table_name} (\r\n\tid int(6) NOT NULL auto_increment COMMENT 'ID',\r\n\tlotto_id int(6) NOT NULL default '0' COMMENT '期数',\r\n\tuser_id int(6) NOT NULL default '0' COMMENT '使用者id',\r\n\tbettype int(2) default NULL COMMENT '下注类别',\r\n\tnumbers varchar(4) NOT NULL COMMENT '下注内容',\r\n\ttotalnum int(7) default NULL COMMENT '笔数',\r\n\tamount1 float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成',\r\n\tamount2 float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成',\r\n\tamount3 float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)',\r\n\tamount4 float(14,3) NOT NULL default '0' COMMENT '累计输赢(占成额*赔率)',\r\n\tback1 float(9,3) NOT NULL default '0.000' COMMENT '注单总退水',\r\n\tback2 float(9,3) NOT NULL default '0.000' COMMENT '管理层退水',\r\n\tback3 float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)',\r\n\trate1 float(9,3) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)',\r\n\trate2 float(9,3) NOT NULL default '0.00' COMMENT '输赢赔率',\r\n\ttotalnuma int(7) default NULL COMMENT '笔数',\r\n\tamount1a float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成',\r\n\tamount2a float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成',\r\n\tamount3a float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)',\r\n\tamount4a float(14,3) default '0' COMMENT '累计输赢(占成额*赔率)',\r\n\tback1a float(9,3) NOT NULL default '0.000' COMMENT '注单总退水',\r\n\tback2a float(9,3) NOT NULL default '0.000' COMMENT '管理层退水',\r\n\tback3a float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)',\r\n\trate1a float(9,3) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)',\r\n\trate2a float(9,3) NOT NULL default '0.00' COMMENT '输赢赔率',\r\n\ttotalnumb int(7) default NULL COMMENT '笔数',\r\n\tamount1b float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成',\r\n\tamount2b float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成',\r\n\tamount3b float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)',\r\n\tamount4b float(14,3) default '0' COMMENT '累计输赢(占成额*赔率)',\r\n\tback1b float(9,3) NOT NULL default '0.000' COMMENT '注单总退水',\r\n\tback2b float(9,3) NOT NULL default '0.000' COMMENT '管理层退水',\r\n\tback3b float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)',\r\n\trate1b float(9,3) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)',\r\n\trate2b float(9,3) NOT NULL default '0.00' COMMENT '输赢赔率',\r\n\ttotalnumc int(7) default NULL COMMENT '笔数',\r\n\tamount1c float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成',\r\n\tamount2c float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成',\r\n\tamount3c float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)',\r\n\tamount4c float(14,3) default '0' COMMENT '累计输赢(占成额*赔率)',\r\n\tback1c float(9,3) NOT NULL default '0.000' COMMENT '注单总退水',\r\n\tback2c float(9,3) NOT NULL default '0.000' COMMENT '管理层退水',\r\n\tback3c float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)',\r\n\trate1c float(9,3) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)',\r\n\trate2c float(9,3) NOT NULL default '0.00' COMMENT '输赢赔率',\r\n\ttotalnumd int(7) default NULL COMMENT '笔数',\r\n\tamount1d float(14,3) NOT NULL default '0' COMMENT '下注累计金额不计算占成',\r\n\tamount2d float(14,3) NOT NULL default '0' COMMENT '下注累计金额计算占成',\r\n\tamount3d float(14,3) NOT NULL default '0' COMMENT '累计输赢金额(下注额*赔率)',\r\n\tamount4d float(14,3) default '0' COMMENT '累计输赢(占成额*赔率)',\r\n\tback1d float(9,3) NOT NULL default '0.000' COMMENT '注单总退水',\r\n\tback2d float(9,3) NOT NULL default '0.000' COMMENT '管理层退水',\r\n\tback3d float(9,3) NOT NULL default '0.000' COMMENT '管理层退水(计占成)',\r\n\trate1d float(9,3) NOT NULL default '0.00' COMMENT '平均赔率(依笔数)',\r\n\trate2d float(9,3) NOT NULL default '0.00' COMMENT '输赢赔率',\r\n\tupdatetime int(11) default '0' COMMENT '更新时间',\r\n\tPRIMARY KEY  (id),\r\n\tUNIQUE KEY lotto_id (lotto_id,user_id,bettype,numbers)\r\n\t) ENGINE=MyISAM;";
    getdb($sql);
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$ui = new UI();
$cmd = getvar("cmd");
$title = $cmd == "add" ? "新增期数" : "颁奖作业";
$list = "<a href=op.php?op=admin&fp=history>列表</a>";
$ui->load("calendar_js" , "calendar_js.htm");
$ui->show("calendar_js");
$conf = new Config();
if (isset($_GET['cmd']) && $_GET['cmd'] == "stopnow1")
{
    $conf->set("linebet", date("Hi", systime() - 60));
    $conf->set("waitbet", date("Hi", systime() - 60));
    $cmd = "edit";
}
if (isset($_GET['cmd']) && $_GET['cmd'] == "stopnow0")
{
    if (date("Hi", systime() - 60) < $conf->get("waitbet"))
    {
        $conf->set("linebet", date("Hi", systime() - 60));
        $conf->set("waitbet", date("Hi", systime() - 60));
    }
    if (date("Hi", systime() - 60) < $conf->get("othebet"))
    {
        $conf->set("othebet", date("Hi", systime() - 60));
    }
    $conf->set("stopbet", date("Hi", systime() - 60));
    $cmd = "edit";
}
switch ($cmd)
{
    case "add" :
    case "edit" :
        echo title($title, $list);
        echo "<p>";
        if (SubAccountRight($curruser, 3) == 2)
        {
            EditLotto($cmd);
        }
        else
        {
            diemsg("Error command!");
        }
        break;
    default :
        diemsg("Error command!");
        break;
}
?>